
#include "http.h"
#include "tool.h"
// #include "stdio.h"
// Function to send HTTP GET request
void ICACHE_FLASH_ATTR send_http_request() {
    os_printf("Sending HTTP request...\n");

    struct espconn *conn = (struct espconn *)os_zalloc(sizeof(struct espconn));
    if (conn == NULL) {
        os_printf("espconn allocation failed\n");
        return;
    }

    // Configure the connection
    conn->type = ESPCONN_TCP;
    conn->state = ESPCONN_NONE;
    conn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
    if (conn->proto.tcp == NULL) {
        os_free(conn);
        os_printf("esp_tcp allocation failed\n");
        return;
    }

    // Resolve the HTTP server's IP address
    ip_addr_t ip;
    err_t res = espconn_gethostbyname(conn, "cyjx.cool", &ip, http_request);
    os_printf("espconn_gethostbyname res	%d\n", res);
    if (res != ESPCONN_OK && res != ESPCONN_INPROGRESS) {
        os_printf("DNS lookup failed\n");
        os_free(conn->proto.tcp);
        os_free(conn);
    }
}

// HTTP request callback
void ICACHE_FLASH_ATTR http_request(const char *name, ip_addr_t *ipaddr, void *arg) {
    os_printf("http_request to do\n");

    struct espconn *conn = (struct espconn *)arg;

    if (ipaddr == NULL) {
        os_printf("DNS lookup failed\n");
        os_free(conn->proto.tcp);
        os_free(conn);
        return;
    }

    os_printf("DNS lookup succeeded. IP=%d.%d.%d.%d\n",
              *((uint8 *) &ipaddr->addr),
              *((uint8 *) &ipaddr->addr + 1),
              *((uint8 *) &ipaddr->addr + 2),
              *((uint8 *) &ipaddr->addr + 3));

    os_memcpy(conn->proto.tcp->remote_ip, &ipaddr->addr, 4);
    conn->proto.tcp->remote_port = 80;
    conn->proto.tcp->local_port = espconn_port();

    espconn_regist_connectcb(conn, connect_callback);
    espconn_regist_reconcb(conn, reconnect_callback);
    espconn_regist_disconcb(conn, disconnect_callback);

    espconn_connect(conn);
}

// Connect callback
void ICACHE_FLASH_ATTR connect_callback(void *arg) {
    os_printf("connect_callback\n");

    struct espconn *conn = (struct espconn *)arg;
    int type = wifi_get_sleep_type();
    int dc = read_voltage();
    os_printf("wifi_get_sleep_type %d\n", type);
    os_printf("dc %d\n", dc);

    char request[250];
    ets_sprintf(request, "GET " HTTP_PATH "?type=%d&dc=%d HTTP/1.1\r\n"
                          "Host: " HTTP_SERVER "\r\n"
                          "Connection: close\r\n\r\n", type, dc);

    os_printf("request %s\n", request);
    espconn_sent(conn, (uint8 *)request, os_strlen(request));
    espconn_regist_sentcb(conn, sent_callback);
    espconn_regist_recvcb(conn, recv_callback);  // Register receive callback
}

// Sent callback
void ICACHE_FLASH_ATTR sent_callback(void *arg) {
    os_printf("HTTP request sent\n");
}
// Receive callback
void ICACHE_FLASH_ATTR recv_callback(void *arg, char *pdata, unsigned short len) {
    os_printf("Received data: %d\n", len);
    struct espconn *conn = (struct espconn *)arg;
    espconn_disconnect(conn);  // Close connection after receiving data
}

// Reconnect callback
void ICACHE_FLASH_ATTR reconnect_callback(void *arg, sint8 err) {
    os_printf("Reconnect callback, error code: %d\n", err);
}

// Disconnect callback
void ICACHE_FLASH_ATTR disconnect_callback(void *arg) {
    os_printf("Disconnected\n");


    struct espconn *conn = (struct espconn *)arg;
    os_free(conn->proto.tcp);
    os_free(conn);
}